﻿#pragma execution_character_set("utf-8")

#include "TalkWindowStacke.h"
#include <windows.h>
#include <windowsx.h>
#include <QtMath>
#include "TalkItem.h"
#include <QDebug>

TalkWindowStacke::TalkWindowStacke(QWidget *parent)
	: QWidget(parent), isPressed(false), isFontShow(false)
{
	ui.setupUi(this);
	this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
	this->setAttribute(Qt::WA_TranslucentBackground);
	this->setAttribute(Qt::WA_DeleteOnClose);

	connect(ui.btn_close, &QPushButton::clicked, this, &TalkWindowStacke::slotCloseBtn);
	connect(ui.btn_Max, &QPushButton::clicked, this, &TalkWindowStacke::slotMaxBtn);
	connect(ui.btn_min, &QPushButton::clicked, this, &TalkWindowStacke::slotMinBtn);
	connect(ui.btn_more, &QPushButton::clicked, this, &TalkWindowStacke::slotMoreBtn);
	connect(ui.FriendListWidget, &QListWidget::itemClicked, this, &TalkWindowStacke::onTalkWindowItemClicked);

	EmojFont_DLL = new Client_Emoj_Font;
	fontDetail = EmojFont_DLL->getFontDetailFromInI();
	connect(EmojFont_DLL, &Client_Emoj_Font::signalEmotionImg, [=](QString fileName) {emit signalEmotionImg(fileName, ui.TalkStackedWidget->currentWidget()); });
	connect(EmojFont_DLL, &Client_Emoj_Font::signalChangeFormatChar, [=](int type,QString str) {
		switch (type)
		{
		case 1:fontDetail.FrontType = str; break;
		case 2:fontDetail.FrontSize = str.toInt(); break;
		case 3:fontDetail.FrontBold = str.toInt(); break;
		case 4:fontDetail.FrontItalic = str.toInt(); break;
		case 5:fontDetail.FrontUnderLine = str.toInt(); break;
		case 6:fontDetail.FrontColor = str.toUInt(NULL, 16); break;
		default:break;
		}
		emit signalUpdateFont();
	});

	ui.e_search->installEventFilter(this);//安装过滤器

	qDebug() << "构造TalkWindowStacke()";
}

void TalkWindowStacke::AddTalkWindow(UserInfo userInfo, QString msg, QString time)
{
	if (m_talkwindowItemMap.contains(userInfo.Account))
	{
		onTalkWindowItemClicked(m_talkwindowItemMap.value(userInfo.Account).Item);
		return;
	}

	TalkWindow* talkWindow = new TalkWindow(userInfo, this);
	ui.TalkStackedWidget->addWidget(talkWindow);

	QListWidgetItem *Item = new QListWidgetItem(ui.FriendListWidget);
	TalkItem *talkItem = new TalkItem();
	talkItem->SetValue(userInfo, msg, time);
	ui.FriendListWidget->setItemWidget(Item, talkItem);//将talkItem赋给该Item

	connect(talkWindow, &TalkWindow::signalShowFont, [=](bool flag, QWidget* sdsd) {
		if (flag)
		{
			isFontShow = true;
			EmojFont_DLL->creatFontWidget(NULL);
		} 
		else
		{
			isFontShow = false;
			EmojFont_DLL->closeFontWidget();
		}
		emit signalShowFont(flag, sdsd);
	});
	connect(talkWindow, &TalkWindow::signalSengMessage, [=](QString flag, QString sdsd) {
		emit signalSengMessage(flag, sdsd);
	});

	connect(this, &TalkWindowStacke::signalShowFont, talkWindow, &TalkWindow::slotShowFront);
	connect(this, &TalkWindowStacke::signalEmotionImg, talkWindow, &TalkWindow::slotGetEmotionImg);
	connect(this, &TalkWindowStacke::signalUpdateFont, talkWindow, &TalkWindow::slotUpdateFont);
	connect(talkWindow, &TalkWindow::signalCloseTalk, this, &TalkWindowStacke::slotCloseChildWidget);
	connect(talkItem, &TalkItem::signalCloseTalk, this, &TalkWindowStacke::slotCloseChildWidget);

	TalkWindowInfo talkWindowInfo;
	talkWindowInfo.userInfo = userInfo;
	talkWindowInfo.Item = Item;
	talkWindowInfo.talkWindow = talkWindow;
	m_talkwindowItemMap.insert(userInfo.Account, talkWindowInfo);

	onTalkWindowItemClicked(Item);
}

void TalkWindowStacke::slotCloseChildWidget(QString account)
{
	TalkWindowInfo info = m_talkwindowItemMap.value(account);
	m_talkwindowItemMap.remove(account);

	ui.TalkStackedWidget->removeWidget(info.talkWindow);
	info.talkWindow->close();
	QListWidgetItem *item = ui.FriendListWidget->takeItem(ui.FriendListWidget->row(info.Item));
	delete item;

	EmojFont_DLL->closeFontWidget();

	if (ui.TalkStackedWidget->count() < 1)
		slotCloseBtn();
	else
		onTalkWindowItemClicked(m_talkwindowItemMap.begin().value().Item);
}

void TalkWindowStacke::onTalkWindowItemClicked(QListWidgetItem *item)
{
	for (auto itor : m_talkwindowItemMap)
	{
		if (itor.Item == item)
		{
			item->setSelected(true);
			ui.TalkStackedWidget->setCurrentWidget(itor.talkWindow);
			ui.l_friendname->setText(itor.userInfo.NickName);
			emit signalShowFont(true, itor.talkWindow);
			break;
		}
	}
}

void TalkWindowStacke::slotSearch()
{
	QString temp = ui.e_search->text();
	qDebug() << "slotSearch()"<< temp;
}

void TalkWindowStacke::slotCloseBtn()
{
	delete EmojFont_DLL;

	m_talkwindowItemMap.clear();
	this->close();
	emit deleteStackeWidget();
}

void TalkWindowStacke::slotMaxBtn()
{
	if (this->isMaximized())
	{
		this->showNormal();
		ui.btn_Max->setStyleSheet("QPushButton#btn_Max{border-image:url(Resources/TalkWindow/max_normal.png);}");
	}
	else
	{
		this->showMaximized(); 
		ui.btn_Max->setStyleSheet("QPushButton#btn_Max{border-image:url(Resources/TalkWindow/restore_normal.png);}");
	}
}

void TalkWindowStacke::slotMinBtn()
{
	this->showMinimized();
}

void TalkWindowStacke::slotMoreBtn()
{
	qDebug() << "slotMoreBtn()";
}

bool TalkWindowStacke::eventFilter(QObject * watched, QEvent * event)
{
	if (watched == ui.e_search && event->type() == QFocusEvent::FocusOut)
		ui.e_search->clear();//清除搜索框中内容

	if (watched == ui.e_search && event->type() == QEvent::KeyPress)
	{
		QKeyEvent *key = static_cast<QKeyEvent*>(event);
		if (key->key() == Qt::Key_Return || key->key() == Qt::Key_Enter)
		{
			slotSearch();
			qDebug() << "回车搜索";
		}
	}
	return QWidget::eventFilter(watched, event);
}

void TalkWindowStacke::paintEvent(QPaintEvent * event)
{
	//绘制窗口边框的阴影部分
	QPainter borderpainter(this);
	QPainterPath Path;
	Path.setFillRule(Qt::WindingFill);
	Path.addRect(5, 5, this->width() - 5, this->height() - 5);
	borderpainter.setRenderHint(QPainter::Antialiasing, true);

	QColor color(0, 0, 0, 10);
	for (int i = 0; i < 5; i++)
	{
		QPainterPath path;
		path.setFillRule(Qt::WindingFill);
		path.addRect(5 - i, 5 - i, this->width() - (5 - i) * 2, this->height() - (5 - i) * 2);
		color.setAlpha(120 - qSqrt(i) * 50);
		borderpainter.setPen(color);
		borderpainter.drawPath(path);
	}
}

void TalkWindowStacke::mousePressEvent(QMouseEvent * event)
{
	if (event->button() == Qt::LeftButton)
	{
		isPressed = true;
		startMovePos = event->globalPos();
		if (ui.e_search->hasFocus())
		{
			QPoint local= ui.e_search->mapToGlobal(ui.e_search->pos());
			if (startMovePos.x() < local.x() || startMovePos.x() > ui.e_search->width() + local.x()||
				startMovePos.y() < local.y() || startMovePos.y() > ui.e_search->height() + local.y())
				ui.e_search->clearFocus();
		}
	}
	return QWidget::mousePressEvent(event);
}

void TalkWindowStacke::mouseMoveEvent(QMouseEvent * event)
{
	if (isPressed)
	{
		QPoint movePoint = event->globalPos() - startMovePos;
		QPoint widgetPos = this->pos() + movePoint;
		startMovePos = event->globalPos();
		this->move(widgetPos.x(), widgetPos.y());
	}
	return QWidget::mouseMoveEvent(event);
}

void TalkWindowStacke::mouseReleaseEvent(QMouseEvent * event)
{
	isPressed = false;
	return QWidget::mouseReleaseEvent(event);
}

bool TalkWindowStacke::nativeEvent(const QByteArray & eventType, void * message, long * result)
{
	MSG* msg = (MSG*)message;
	switch (msg->message)
	{
	case WM_NCHITTEST:
		int xPos = GET_X_LPARAM(msg->lParam) - this->frameGeometry().x();
		int yPos = GET_Y_LPARAM(msg->lParam) - this->frameGeometry().y();
		if (this->childAt(xPos, yPos) == 0)
			*result = HTCAPTION;
		else
			return false;

		if (xPos > 0 && xPos < 4)//拉伸左边
			*result = HTLEFT;
		if (xPos > (this->width() - 4) && xPos < this->width())//拉伸右边
			*result = HTRIGHT;
		if (yPos > 0 && yPos < 4)//拉伸上边
			*result = HTTOP;
		if (yPos > (this->height() - 4) && yPos < this->height())//拉伸下边
			*result = HTBOTTOM;
		if (xPos > 0 && xPos < 12 && yPos > 0 && yPos < 12)//拉伸左上边
			*result = HTTOPLEFT;
		if (xPos > (this->width() - 12) && xPos < this->width()  && yPos > 0 && yPos < 12)//拉伸右上边
			*result = HTTOPRIGHT;
		if (xPos > 0 && xPos < 12 && yPos >(this->height() - 12) && yPos < this->height())//拉伸左下边
			*result = HTBOTTOMLEFT;
		if (xPos > (this->width() - 12) && xPos < this->width() && yPos >(this->height() - 12) && yPos < this->height())//拉伸右下边
			*result = HTBOTTOMRIGHT;
		return true;
	}
	return false;
}

TalkWindowStacke::~TalkWindowStacke()
{
	qDebug() << "析构~TalkWindowStacke()";
}